package com.lw.controller;

import cn.dev33.satoken.annotation.*;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lw.common.utils.ResultVo;
import com.lw.common.vo.UserVo;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.extensions.Extension;
import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import jakarta.annotation.Resource;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lw.entity.TbUser;
import com.lw.service.TbUserService;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/**
 * 控制层。
 *
 * @author lw
 * @since 2024年08月03日 18:20:52
 */
@RestController
@RequestMapping("/tbUser")
@Tag(name = "A-用户接口", description = "用户接口")
public class TbUserController {

    @Resource
    private TbUserService tbUserService;

    @Resource
    CacheManager cacheManager;

    /**
     * todo 经典案例
     * 缓存查询案例
     */
    @GetMapping("cacheTest")
    public String cacheTest() {
        System.out.println("=========================================================");
        Collection<String> cacheNames = cacheManager.getCacheNames();
        for (String cacheName : cacheNames) {
            System.out.println("缓存key值:" + cacheName);
            Cache cache = cacheManager.getCache(cacheName);
            List login = cache.get("10001", List.class);
            System.out.println("缓存数据:" + login.toString());
            System.out.println("=========================================================");
        }
        return "获取缓存成功";
    }

    @SaIgnore
    @GetMapping("doLogin")
    public ResultVo<String> doLogin(String username, String password) {
        if ("lw".equals(username) && "123".equals(password)) {
            StpUtil.login(10001);
            return ResultVo.success("登录成功");
        }

        return ResultVo.error("登录失败");
    }

    @SaCheckPermission("tbUser.isLogin")
    @GetMapping("isLogin")
    public ResultVo<String> isLogin() {
        String msg = String.format("当前会话是否登录：%s", StpUtil.isLogin());
        return ResultVo.success(msg);
    }

    @SaCheckPermission("tbUser.tokenInfo")
    @RequestMapping("tokenInfo")
    public ResultVo<String> tokenInfo() {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return ResultVo.success(tokenInfo.toString());
    }

    @RequestMapping("logout")
    public ResultVo<String> logout() {
        StpUtil.logout(10001);
        return ResultVo.success("退出登录成功");
    }

    // 权限校验：必须具有指定权限才能进入该方法
    @SaCheckRole("admin")
    @RequestMapping("add")
    public String add() {
        return "用户增加";
    }

    // 二级认证校验：必须二级认证之后才能进入该方法
    @SaCheckSafe()
    @RequestMapping("add2")
    public String add2() {
        return "用户增加";
    }


    @SaCheckLogin
    @GetMapping("getUserDetail")
    public ResultVo<List<UserVo>> getUserDetail() {
        List<UserVo> list = tbUserService.getUserDetail();
        return ResultVo.success(list);
    }

    /**
     * 添加。
     *
     * @param tbUser
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    public boolean save(@RequestBody TbUser tbUser) {
        return tbUserService.save(tbUser);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Integer id) {
        return tbUserService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param tbUser
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody TbUser tbUser) {
        return tbUserService.updateById(tbUser);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<TbUser> list() {
        return tbUserService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public TbUser getInfo(@PathVariable Integer id) {
        return tbUserService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<TbUser> page(Page<TbUser> page) {
        return tbUserService.page(page);
    }

}
